package gcb.metalsys.service.cop.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import gcb.metalsys.entity.auth.AuthUser;
import gcb.metalsys.entity.cop.CopDepartment;
import gcb.metalsys.entity.cop.CopWarehouse;
import gcb.metalsys.entity.cop.ProTransfer;
import gcb.metalsys.mapper.cop.CopWarehouseMapper;
import gcb.metalsys.service.auth.AuthUserService;
import gcb.metalsys.service.cop.CopDepartmentService;
import gcb.metalsys.service.cop.CopWarehouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2020-07-15
 */
@Service
public class CopWarehouseServiceImpl extends ServiceImpl<CopWarehouseMapper, CopWarehouse> implements CopWarehouseService {

    @Autowired
    private CopWarehouseMapper copWarehouseMapper;
    @Autowired
    private AuthUserService authUserService;
    @Autowired
    private CopDepartmentService copDepartmentService;

    @Override
    public List<CopWarehouse> countTableListMonthOutById(Long departmentId) {
        List<CopWarehouse> tableMonthOutList;
        tableMonthOutList = copWarehouseMapper.countTableListMonthOutById(departmentId);
        return tableMonthOutList;
    }

    @Override
    public List<CopWarehouse> getPieListById(Long departmentId) {
        List<CopWarehouse> pieList;
        pieList = copWarehouseMapper.selectPieListById(departmentId);
        return pieList;
    }

    @Override
    public boolean warehouseTransfer(ProTransfer proTransfer) {

        QueryWrapper<CopWarehouse> outQueryWrapper = new QueryWrapper<>();
        outQueryWrapper.eq("user_id",proTransfer.getOutUserId());
        outQueryWrapper.eq("product_id",proTransfer.getProductId());
        CopWarehouse outWarehouse = getOne(outQueryWrapper);

        if (outWarehouse.getMetalSize() < proTransfer.getTransferNumber()) {
            return false;
        }else{
            outWarehouse.setMetalSize(outWarehouse.getMetalSize() - proTransfer.getTransferNumber());
        }

        QueryWrapper<CopWarehouse> inQueryWrapper = new QueryWrapper<>();
        inQueryWrapper.eq("user_id",proTransfer.getInUserId());
        inQueryWrapper.eq("product_id",proTransfer.getProductId());
        CopWarehouse inWarehouse = getOne(inQueryWrapper);

        AuthUser inUser = authUserService.getById(proTransfer.getInUserId());

        if(inWarehouse == null){
            CopWarehouse newInWarehouse = new CopWarehouse(proTransfer.getInUserId(),
                    inUser.getUserDepartment(),proTransfer.getProductId(), proTransfer.getTransferNumber());
            save(newInWarehouse);
        }else{
            inWarehouse.setMetalSize(inWarehouse.getMetalSize() + proTransfer.getTransferNumber());
            updateById(inWarehouse);
        }
        updateById(outWarehouse);
        return true;
    }

    public boolean plusWarehouse(Long productId,Long userId,int number){
        //查看库存
        QueryWrapper<CopWarehouse> outQueryWrapper = new QueryWrapper<>();
        outQueryWrapper.eq("user_id",userId);
        outQueryWrapper.eq("product_id",productId);
        CopWarehouse warehouse = getOne(outQueryWrapper);
        if (warehouse == null){
            CopWarehouse newInWarehouse = new CopWarehouse();
            newInWarehouse.setMetalSize(number);
            newInWarehouse.setUserId(userId);
            newInWarehouse.setProductId(productId);
            //查看部门
            QueryWrapper<CopDepartment> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id",userId);
            CopDepartment department = copDepartmentService.getOne(queryWrapper);
            newInWarehouse.setDepartmentId(department.getDepartmentId());
            save(newInWarehouse);
        }else{
            warehouse.setMetalSize(warehouse.getMetalSize() + number);
            updateById(warehouse);
        }
        return true;
    }

    public boolean subtractWarehouse(Long productId,Long userId,int number){
        QueryWrapper<CopWarehouse> outQueryWrapper = new QueryWrapper<>();
        outQueryWrapper.eq("user_id",userId);
        outQueryWrapper.eq("product_id",productId);
        CopWarehouse warehouse = getOne(outQueryWrapper);
        if (warehouse == null){
            return false;
        }else{
            warehouse.setMetalSize(warehouse.getMetalSize() - number);
            updateById(warehouse);
        }
        return true;
    }


}
